<template>
  <div class="mainContainer">
    <!-- Search start -->
    <el-form
      :inline="true"
      size="small"
      :model="queryParams"
      class="demo-form-inline searchContainer"
    >
      <el-form-item label="SQL编号: ">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入SQL编号..."
          @keyup.enter="onSearch"
        />
      </el-form-item>
      <el-form-item label="SQL类型:">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择SQL类型..."
          collapse-tags
          clearable
          style="width: 150px"
        >
          <el-option label="查询" value="1"></el-option>
          <el-option label="更新" value="2"></el-option>
          <el-option label="插入" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="执行人:">
        <el-select
          multiple
          v-model="queryParams.executorIdList"
          placeholder="请选择SQL执行人..."
          collapse-tags
          clearable
          style="width: 150px"
        >
          <el-option
            v-for="item in userListAll"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          class="bs-form-btn"
          size="small"
          type="primary"
          @click="onSearch"
          ><el-icon><Search /></el-icon>查 询</el-button
        >
        <ResetButton :queryParams="queryParams" />
      </el-form-item>
    </el-form>
    <!-- Search end -->

    <!-- Table start -->
    <div class="dataContainer">
      <el-table
        ref="tableRef"
        v-loading="tableLoading"
        :border="true"
        class="bs-table"
        :data="tableData"
        :header-cell-style="{ textAlign: 'center' }"
        :cell-style="{ textAlign: 'center' }"
        table-layout='auto'
      >
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column prop="code" label="sql编号" width="180" />
        <el-table-column prop="type" label="sql类型" width="120">
          <template #default="scope">
            {{ getSqlType(scope.row.type) }}
          </template>
        </el-table-column>
        <el-table-column label="sql语句详情" width="380">
          <template #default="scope">
            <div class="sqlDetail">
              <el-popover
                effect="dark"
                placement="right"
                title=""
                trigger="hover"
                :width="400"
                :content="scope.row.sqlText"
                popper-class="define-popper"
                :teleported="true" 
              >
                <template #reference>
                  <div>{{ scope.row.sqlText }}</div>
                </template>
              </el-popover>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="运行时参数" width="380">
          <template #default="scope">
            <div class="sqlDetail">
              <el-popover
                effect="dark"
                placement="right"
                title=""
                trigger="hover"
                :width="400"
                :content="scope.row.params"
                popper-class="define-popper"
                :teleported="true" 
              >
                <template #reference>
                  <div>{{ scope.row.params }}</div>
                </template>
              </el-popover>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="effectCount" label="影响行数" width="120" />
        <el-table-column prop="executorName" label="执行人" width="120" />
        <el-table-column prop="executorTime" label="执行时间" />
      </el-table>
      <pagination
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        small="small"
        disabled="disabled"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableTotal"
        @pagination="selectByPage"
      />
    </div>
    <AddOrUpdateSql ref="addOrUpdateSqlRef" @flushTable="onSearch" />
    <!-- sql试运行抽屉 -->
    <SqlRunWindowsVue ref="sqlRunRef" />
  </div>
</template>

<script>
import { selectByPage } from "@/api/system/runSqlRecord";
import { userListAll } from "@/api/system/user";
export default {
  data() {
    return {
      tableTotal: 0,
      tableLoading: false,
      tableData: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 500,
        code: "",
        type: "",
        executorIdList:[]
      },
      // 用户下拉框
      userListAll:[]
    };
  },
  created() {
    this.selectByPage();
    this.selectUserList()
  },
  methods: {
    selectUserList(){
      userListAll().then(res=>{
        if (res.code == 1) {
          this.userListAll = res.data
        }
      })
    },
    selectByPage() {
      this.tableLoading = true;
      selectByPage(this.queryParams)
        .then((res) => {
          this.tableLoading = false;
          if (res.code === 1) {
            this.tableData = res.data.records;
            this.tableTotal = res.data.total;
          }
        })
        .catch((error) => {
          this.tableLoading = false;
        });
    },
    // 搜索
    onSearch() {
      this.queryParams.pageNo = 1;
      this.selectByPage();
    },
    // 获取类型名称
    getSqlType(type) {
      if (type === 1) {
        return "查询";
      } else if (type === 2) {
        return "更新";
      } else if (type === 3) {
        return "插入";
      } else {
        return "--";
      }
    },
  },
};
</script>

<style></style>
